<?php
global $_GPC, $_W;
$GLOBALS['frames'] = $this->getNaveMenu();
                $user=pdo_get('users',array('username'=>$_W['username']));
                $uniacid=pdo_get('users_permission',array('uid'=>$user['uid']));
$admin = pdo_get('zh_yjm_store',array('admin'=>$user['username']));

$pageindex = max(1, intval($_GPC['page']));
$pagesize=10;
$where=' WHERE  uniacid=:uniacid  ';
$data[':uniacid']=$uniacid['uniacid'];

if(isset($_GPC['keywords'])){
    $where.="  and (name LIKE  concat('%', :name,'%') or phone LIKE  concat('%', :name,'%'))";
    $data[':name']=$_GPC['keywords']; 
}

$sql="SELECT * FROM ".tablename('zh_yjm_cusmember') .$where." and store_id=".$admin['id']."  ORDER BY time desc";
$total=pdo_fetchcolumn("SELECT count(*) FROM ".tablename('zh_yjm_cusmember').$where." and store_id=".$admin['id']."  ORDER BY time desc",$data);

$select_sql =$sql." LIMIT " .($pageindex - 1) * $pagesize.",".$pagesize;
$cusmember=pdo_fetchall($select_sql,$data);
$pager = pagination($total, $pageindex, $pagesize);

// $cusmember = pdo_fetchall("SELECT * FROM " . tablename('zh_yjm_cusmember'). "  WHERE uniacid = ".$_W['uniacid']. "  ORDER BY sort DESC");
// for ($i=0; $i <count($cusmember) ; $i++) { 
// 	$cusmember[$i]['cusmembertypes']=pdo_get('zh_yjm_cusmembertype',array('id'=>$cusmember[$i]['cusmembertype_id']));
// 	$cusmember[$i]['cusmembertype']=$cusmember[$i]['cusmembertypes']['title'];
// }
// $assistant = pdo_fetchall("SELECT * FROM " . tablename('zh_yjm_assistant'));
$assistant = pdo_fetchall("SELECT * FROM " . tablename('zh_yjm_assistant'). "  WHERE uniacid = ".$uniacid['uniacid']. "  and store_id=".$admin['id']."  ORDER BY sort DESC");
// $projecttype = pdo_fetchall("SELECT * FROM " . tablename('zh_yjm_projecttype'));
$projecttype = pdo_fetchall("SELECT * FROM " . tablename('zh_yjm_projecttype'). "  WHERE uniacid = ".$uniacid['uniacid']. "  and store_id=".$admin['id']."  ORDER BY sort DESC");
// $project = pdo_fetchall("SELECT * FROM " . tablename('zh_yjm_project'));
$project = pdo_getall('zh_yjm_project',array('uniacid'=>$uniacid['uniacid'],'projecttype_id'=>$projecttype[0]['id']),array(),'','sort asc');
$op = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
if($op=='delete'){
	$res=pdo_delete('zh_yjm_cusmember',array('id'=>$_GPC['id']));
	// var_dump($res);die;
	if($res){
	  message('删除成功',$this->createWebUrl2('cusmember',array()),'success');
	}else{
	  message('删除失败','','error');
	}
}
if($_GPC['op']=='hoty'){
	$res=pdo_update('zh_yjm_cusmember',array('state'=>0),array('id'=>$_GPC['id']));
	if($res){
     message('', 'referer', 'success');
		// iajax('200','成功',$this->createWebUrl2('activecusmember'), 'ajax', false);
    }else{
          message('失败！','','error');
    }
}
if($_GPC['op']=='hotx'){
	$res=pdo_update('zh_yjm_cusmember',array('state'=>1),array('id'=>$_GPC['id']));
	if($res){
     message('', 'referer', 'success');
    }else{
          message('失败！','','error');
    }
}
if(checksubmit('submit2')){
      global $_W,$_GPC;
      $openid=pdo_get('zh_yjm_cusmember',array('id'=>$_GPC['id']));
      $money=$openid['money']+$_GPC['money'];
      $res=pdo_update('zh_yjm_cusmember',array('money'=>$money),array('id'=>$_GPC['id']));


        $datas['m_id']=$openid['id'];
        $datas['ass_id']=$_GPC['ass_id'];
        $datas['money']=$_GPC['money'];
        $datas['time']=date('Y-m-d H:i:s',time());
        $datas['uniacid']=$uniacid['uniacid'];
        pdo_insert('zh_yjm_moneyinfo',$datas);
        if($res){
          message('', 'referer', 'success');
        }else{
          message('充值失败！','','error');
        }
}
if(checksubmit('submit3')){
      global $_W,$_GPC;
      $openid=pdo_get('zh_yjm_cusmember',array('id'=>$_GPC['id']));
      $money=$openid['money']-$_GPC['money'];
      $res=pdo_update('zh_yjm_cusmember',array('money'=>$money),array('id'=>$_GPC['id']));
      // var_dump($_GPC['projecttype_id']);die;

        $datax['m_id']=$openid['id'];
        $datax['ass_id']=$_GPC['ass_id'];
        $datax['projecttype_id']=$_GPC['projecttype_id'];
        $datax['project_id']=$_GPC['project_id'];
        $datax['money']=$_GPC['money'];
        $datax['time']=date('Y-m-d H:i:s',time());
        $datax['uniacid']=$uniacid['uniacid'];
        pdo_insert('zh_yjm_moneyinfox',$datax);
        if($res){
          message('', 'referer', 'success');
        }else{
          message('消费失败！','','error');
        }
}
include $this->template('web/cusmember');